home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / perlnt.zip / t / io / filetest.nt < prev    next >
Text File  |  1993-07-25  |  5KB  |  182 lines

  1. #
  2. # Test various file test operators.
  3. # Also test file handle stuff that isn't tested elsewhere.
  4. #
  5.  
  6. print "1..30\n";
  7.  
  8. $tfile = "test.tmp";
  9.  
  10. if (-e $tfile)  {
  11.     chmod (0666, $tfile);
  12.     unlink $tfile;
  13. }
  14. open (T, ">$tfile") || die "can't open temp file\n";
  15.  
  16. print T "Test data\n";
  17.  
  18. close(T);
  19.  
  20. #
  21. # Check that -r/-R and -w/-W do the same thing
  22. #
  23. # N.B. ownership tests are meaningless right now!
  24. #
  25.  
  26. if (-w "$tfile") {print "ok 1\n";} else {print "not ok 1\n";}
  27. if (-r "$tfile") {print "ok 2\n";} else {print "not ok 2\n";}
  28. if (-o "$tfile") {print "ok 3\n";} else {print "not ok 3\n";}
  29. if (-R "$tfile") {print "ok 4\n";} else {print "not ok 4\n";}
  30. if (-W "$tfile") {print "ok 5\n";} else {print "not ok 5\n";}
  31. if (-O "$tfile") {print "ok 6\n";} else {print "not ok 6\n";}
  32.  
  33. #
  34. # make sure that -x works only on extension, not modes
  35. #
  36.  
  37. chmod (0777, $tfile);
  38. if (-x "$tfile") {print "not ok 7\n";} else {print "ok 7\n";}
  39. if (-X "$tfile") {print "not ok 8\n";} else {print "ok 8\n";}
  40.  
  41. unlink "foo.exe" if -e "foo.exe";
  42. open(T, ">foo.exe") || die "can't open foo.exe for writing\n";
  43. print T "foo";
  44. close(T);
  45.  
  46. #
  47. # check various extension types
  48. # N.B. NTFS has an execute permission. We'll need to insure that this
  49. # is honored later.
  50. #
  51. if (-x "foo.exe") {print "ok 9\n";} else {print "not ok 9\n";}
  52. rename ("foo.exe", "foo.cmd") || die "Can't rename foo.exe to foo.cmd\n";
  53. if (-x "foo.cmd") {print "ok 10\n";} else {print "not ok 10\n";}
  54. rename ("foo.cmd", "foo.bat") || die "Can't rename foo.cmd to foo.bat\n";
  55. if (-x "foo.bat") {print "ok 11\n";} else {print "not ok 11\n";}
  56. rename ("foo.bat", "foo.com") || die "Can't rename foo.bat to foo.com\n";
  57. if (-x "foo.com") {print "ok 12\n";} else {print "not ok 12\n";}
  58.  
  59. unlink "foo.com";
  60.  
  61. #
  62. # check that writability is affected when we turn off the write bits
  63. #
  64.  
  65. chmod (0111, $tfile);
  66. if (-w "$tfile") {print "not ok 13\n";} else {print "ok 13\n";}
  67. if (-W "$tfile") {print "not ok 14\n";} else {print "ok 14\n";}
  68.  
  69. #
  70. # Check that these don't work!
  71. #
  72.  
  73. if (-S $tfile) {print "not ok 15\n";} else {print "ok 15\n";}
  74. if (-l $tfile) {print "not ok 16\n";} else {print "ok 16\n";}
  75. if (-p $tfile) {print "not ok 17\n";} else {print "ok 17\n";}
  76.  
  77. #
  78. # Test Binary vs. Text test
  79. #
  80.  
  81. if (-B $tfile) {print "not ok 18\n";} else {print "ok 18\n";}
  82. if (-T $tfile) {print "ok 19\n";} else {print "not ok 19\n";}
  83.  
  84. chmod (0666, $tfile);
  85. unlink $tfile;
  86.  
  87. open (T, ">$tfile") || die "Can't create $tfile\n";
  88. binmode(T);
  89. print T "\001\002\003\004\005\006\007\010\011\012\013";
  90. close (T);
  91.  
  92. if (-B $tfile) {print "ok 20\n";} else {print "not ok 20\n";}
  93. if (-T $tfile) {print "not ok 21\n";} else {print "ok 21\n";}
  94.  
  95. if (-e $tfile) {print "ok 22\n";} else {print "not ok 22\n";}
  96. if (-s $tfile) {print "ok 23\n";} else {print "not ok 23\n";}
  97.  
  98. truncate($tfile, 10) || die "can't truncate $tfile to 10 bytes\n";
  99. ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size) = stat($tfile);
  100. if ($size == 10) {print "ok 24\n";} else {print "not ok 24\n";}
  101.  
  102. truncate($tfile, 0) || die "can't truncate $tfile to 0 bytes\n";
  103. if (-z $tfile) {print "ok 25\n";} else {print "not ok 25\n";}
  104. open (T, ">$tfile") || die "can't open $tfile\n";
  105. print T "foo\n";
  106. close(T);
  107. if (-z $tfile) {print "not ok 26\n";} else {print "ok 26\n";}
  108.  
  109. if (-f $tfile) {print "ok 27\n";} else {print "not ok 27\n";}
  110.  
  111. unlink $tfile;
  112.  
  113. #
  114. # check that -t works with STDIN being a file (i.e. returns false)
  115. #
  116.  
  117. open (T, ">$tfile") || die "Can't open $tfile for writing\n";
  118. print T 'if (-t) {print "not";} else {print "ok";}';
  119. close T;
  120. $x = `perl $tfile <$tfile`;
  121. if ($x eq "ok") {print "ok 28\n";} else {print "not ok 28\n";}
  122.  
  123. unlink $tfile;
  124.  
  125. #
  126. # check that the Here-is operator works
  127. #
  128. open (T, ">$tfile") || die "Can't open $tfile for writing: $!\n";
  129. print T <<"HERE_IS_TEST";
  130. This is a test of the here-is mechanism. 
  131. It was written to a file named $tfile
  132. HERE_IS_TEST
  133. close T;
  134.  
  135. open (T, $tfile) || die "Can't open $tfile for reading: $!\n";
  136. @lines = <T>;
  137. close T;
  138. if (@lines == 2 &&
  139.     split(/ /, @lines[1]) &&
  140.     chop ($_[$#_]) &&
  141.     $_[$#_] eq $tfile) {
  142.     print ("ok 29\n");
  143. }
  144. else {
  145.     print "not ok 29\n";
  146. }
  147.  
  148.  
  149. #
  150. # check that the data file handle works
  151. #
  152.  
  153. @a = (1, 2, 3, 4, 5);
  154. $ok = 1;
  155.  
  156. while (<DATA>) {
  157.     if ($a[0] == $_) {
  158.     shift(@a);
  159.     }
  160.     else {
  161.     $ok = 0;
  162.     last;
  163.     }
  164. }
  165. if ($ok) {print "ok 30\n";} else {print "not ok 30\n";}
  166.  
  167. #
  168. # Don't delete the numbers after the end token! They're what
  169. # the DATA file handle is reading!!!
  170. #
  171. __END__
  172. 1
  173. 2
  174. 3
  175. 4
  176. 5
  177.  
  178.     
  179.     
  180.  
  181.